<%@page import="javax.sql.rowset.WebRowSet" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.nio.charset.StandardCharsets"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>submitOrder</title>
</head>
<body>
<%
	//接收from数据
	request.setCharacterEncoding("UTF-8");
	String meal_name = request.getParameter("meal_name");
	String customer_name = request.getParameter("customer_name");
	String meal_price = request.getParameter("meal_price");
    String restaurant_name = request.getParameter("restaurant_name");


	//连接数据库
	Class.forName("org.mariadb.jdbc.Driver");
	Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/mealdb?user=root&password=1230321");
	Statement stmt = conn.createStatement();


	//构造SQL写入数据到数据表
	String sql = "INSERT INTO cart(meal_name, meal_price,customer_name) VALUES(?, ?, ?)";
	PreparedStatement pstmt = conn.prepareStatement(sql);
	pstmt.setString(1, meal_name);
	pstmt.setString(2, meal_price);
	pstmt.setString(3, customer_name);
	ResultSet rs = pstmt.executeQuery();

	stmt.close();
	pstmt.close();
	conn.close();
	//String topage ="restaurantIndex.jsp?customer_name="+customer_name+"&restaurant_name="+restaurant_name+"";
	//返回电池页面restaurant_name
	//response.sendRedirect("restaurantIndex.jsp?customer_name="+customer_name+"&restaurant_name="+restaurant_name);
	// 对顾客名字进行编码
        String encodedCustomerName = URLEncoder.encode(customer_name, StandardCharsets.UTF_8);
        // 对餐厅名字进行编码
        String encodedRestaurantName = URLEncoder.encode(restaurant_name, StandardCharsets.UTF_8);

        // 构建编码后的URL
        String encodedUrl = "restaurantIndex.jsp?customer_name=" + encodedCustomerName + "&restaurant_name=" + encodedRestaurantName;

        System.out.println(encodedUrl);
	
	response.sendRedirect("restaurantIndex.jsp?customer_name="+encodedCustomerName+"&restaurant_name="+encodedRestaurantName);
	%>
	
</body>
</html>